package com.nchu.commondevelopmentframework.controller;

import com.nchu.commondevelopmentframework.common.constant.ApiCodeConstant;
import com.nchu.commondevelopmentframework.common.domain.ApiResult;
import com.nchu.commondevelopmentframework.converter.MenuConverter;
import com.nchu.commondevelopmentframework.entity.Menu;
import com.nchu.commondevelopmentframework.entity.RoleMenu;
import com.nchu.commondevelopmentframework.service.MenuService;
import com.nchu.commondevelopmentframework.service.RoleMenuService;
import com.nchu.commondevelopmentframework.vo.MenuPermsIdNameVO;
import com.nchu.commondevelopmentframework.vo.UserMenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @className: MenuController
 * @description: 菜单控制类
 * @author: Li Chuanwei
 * @date: 2024/02/20 14:39
 * @Company: Copyright [日期] by [作者或个人]
 **/
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService<Menu> menuService;

    @Autowired
    private RoleMenuService<RoleMenu> roleMenuService;

    @GetMapping ("/getMenuByUserId")
    public ApiResult<List<UserMenuVO>> getMenuByUserId(@RequestParam(value = "userId") String userId) {
        List<Menu> menus = menuService.selectMenusByUser(Long.valueOf(userId));
        List<UserMenuVO> userMenuVO = MenuConverter.INSTANCE.menusToUserMenuVOs(menus);;
        return ApiResult.success(userMenuVO);
    }

    @GetMapping("/getAllMenus")
    public ApiResult<List<MenuPermsIdNameVO>> getAllMenus(@RequestParam(value = "roleId") String roleId) {
        List<Menu> menus = menuService.selectAllMenus();
        List<MenuPermsIdNameVO> menuPermsIdNameVOs = MenuConverter.INSTANCE.menusToMenuPermsIdNameVOs(menus);
        for(MenuPermsIdNameVO menuPermsIdNameVO : menuPermsIdNameVOs){
            menuPermsIdNameVO.setIsAllow(roleMenuService.isRoleMenuExist(Long.valueOf(roleId), menuPermsIdNameVO.getMenuId()));
            for(MenuPermsIdNameVO child : menuPermsIdNameVO.getChildren()){
                child.setIsAllow(roleMenuService.isRoleMenuExist(Long.valueOf(roleId), child.getMenuId()));
            }
        }
        return ApiResult.success(menuPermsIdNameVOs);
    }

    @PostMapping("/addRoleMenu")
    public ApiResult<Boolean> addRoleMenu(@RequestBody RoleMenu roleMenu) {
        Boolean isRoleMenuExist = roleMenuService.isRoleMenuExist(roleMenu.getRoleId(), roleMenu.getMenuId());
        if(isRoleMenuExist){
            return ApiResult.fail(ApiCodeConstant.ROLE_MENU_EXIST,"该角色菜单已存在");
        }
        return roleMenuService.save(roleMenu) ? ApiResult.success(true) : ApiResult.fail(ApiCodeConstant.ROLE_MENU_ADD_FAILED,"添加失败");
    }

    @PostMapping("/delRoleMenu")
    public ApiResult<Boolean> delRoleMenu(@RequestBody RoleMenu roleMenu) {
        Boolean isRoleMenuExist = roleMenuService.isRoleMenuExist(roleMenu.getRoleId(), roleMenu.getMenuId());
        if(!isRoleMenuExist){
            return ApiResult.fail(ApiCodeConstant.ROLE_MENU_NOT_EXIST,"该角色菜单不存在");
        }
        return roleMenuService.removeOne(roleMenu) ? ApiResult.success(true) : ApiResult.fail(ApiCodeConstant.ROLE_MENU_DEL_FAILED,"删除失败");
    }
}
